package com.skcc.ts.nsi.service;

import android.util.Log;

import com.skcc.ts.nsi.vo.MEAcntVo;
import com.skcc.ts.nsi.vo.MECardVo;
import com.skcc.ts.nsi.vo.MEExpVo;
import com.skcc.ts.nsi.vo.MEPjtVo;
import com.skcc.ts.nsi.vo.MEUsrVO;

public class MEDataHandler {

	public static MEUsrVO userInfo(String param) {// 사용자 로그인정보(id, name, pw, connection time)
		MEUsrVO usrVo = null;

		if (checkSuccess(param)) {
			String result[] = parseResultString(param, "\\|");
			usrVo = new MEUsrVO();
			usrVo.setMy_id(result[0]);
			usrVo.setMy_name(result[1]);
			usrVo.setMy_pw(result[2]);
			// usrVo.setMy_role(result[4]);
		}
		param = null;
		return usrVo;
	}

	public static MEUsrVO teamInfo(String param) {
		MEUsrVO usrVo = null;

		String usrId = "";
		String usrName = "";
		String usrRole = "";

		if (checkSuccess(param)) {
			String result[][] = parsePrint(param);
			for (int i = 0; i < result.length; i++) {
				for (int j = 0; j < result[i].length; j++) {
					if (j == 0) {
						usrId = usrId.concat(result[i][j] + ",");
					} else if (j == 1) {
						usrName = usrName.concat(result[i][j] + ",");
					} else if (j == 4) {
						usrRole = usrRole.concat(result[i][j] + ",");
					}
				}
			}
			result = null;
			String[] usr_id = checkArray(usrId);
			String[] usr_name = checkArray(usrName);
			String[] usr_role = checkArray(usrRole);

			usrVo = new MEUsrVO();
			usrVo.setUsr_id(usr_id);
			usrVo.setUsr_name(usr_name);
			usrVo.setUsr_role(usr_role);
		}
		param = null;
		return usrVo;
	}

	public static MEPjtVo pjtInfo(String param) {
		MEPjtVo pjtVo = null;

		String pjtCode = "";
		String pjtName = "";
		String pjtStartDay = "";
		String pjtEndDay = "";

		if (checkSuccess(param)) {
			String result[][] = parsePrint(param);
			for (int i = 0; i < result.length; i++) {
				for (int j = 0; j < result[i].length; j++) {
					if (j == 0) {
						pjtCode = pjtCode.concat(result[i][j] + ",");
					} else if (j == 1) {
						pjtName = pjtName.concat(result[i][j] + ",");
					} else if (j == 2) {
						pjtStartDay = pjtStartDay.concat(result[i][j] + ",");
					} else if (j == 3) {
						pjtEndDay = pjtEndDay.concat(result[i][j] + ",");
					}
				}
			}
			result = null;
			String[] pjt_code = checkArray(pjtCode);
			String[] pjt_name = checkArray(pjtName);
			String[] pjt_sday = checkArray(pjtStartDay);
			String[] pjt_eday = checkArray(pjtEndDay);

			pjtVo = new MEPjtVo();
			pjtVo.setPjt_code(pjt_code);
			pjtVo.setPjt_name(pjt_name);
			pjtVo.setPjt_start(pjt_sday);
			pjtVo.setPjt_end(pjt_eday);
		}
		param = null;
		return pjtVo;
	}

	public static MEExpVo expInfo(String param) {
		MEExpVo expVo = null;
		String day = "";// String처리
		String expTime = "";// 정산일자
		String expNumber = "";// 프로젝트코드
		String expPname = "";// 프로젝트명
		String expAcode = "";// 계정코드
		String expAname = "";// 계정명
		String expMoney = "";// 사용금액
		String expSerial = "";// 정산번호

		// Log.d("expInfo!!!", "yes");
		if (checkSuccess(param)) {
			String result[][] = parsePrint(param);
			for (int i = 0; i < result.length; i++) {
				for (int j = 0; j < result[i].length; j++) {
					if (j == 0) {
						day = day.concat(result[i][j].substring(0, 4) + "/" + result[i][j].substring(5, 7) + "/"
								+ result[i][j].substring(8, 10));
						expTime = expTime.concat(day + ",");
						day = "";
					} else if (j == 1) {
						expNumber = expNumber.concat(result[i][j] + ",");
					} else if (j == 2) {
						expPname = expPname.concat(result[i][j] + ",");
					} else if (j == 3) {
						expAcode = expAcode.concat(result[i][j] + ",");
					} else if (j == 4) {
						expAname = expAname.concat(result[i][j] + ",");
					} else if (j == 5) {
						expMoney = expMoney.concat(result[i][j] + ",");
					} else if (j == 6) {
						expSerial = expSerial.concat(result[i][j] + ",");
					}
				}
			}
			result = null;
			String[] exp_time = checkArray(expTime);
			String[] exp_number = checkArray(expNumber);
			String[] exp_pname = checkArray(expPname);
			String[] exp_acode = checkArray(expAcode);
			String[] exp_acname = checkArray(expAname);
			String[] exp_money = checkArray(expMoney);
			String[] exp_serial = checkArray(expSerial);
			// Log.d("exp_serial",exp_serial[0]);
			expVo = new MEExpVo();
			expVo.setExp_time(exp_time);
			expVo.setExp_number(exp_number);
			expVo.setExp_pjtname(exp_pname);
			expVo.setExp_acntcode(exp_acode);
			expVo.setExp_acntname(exp_acname);
			expVo.setExp_money(exp_money);
			expVo.setExp_serial(exp_serial);
		}

		param = null;
		return expVo;
	}

	public static MEAcntVo acntInfo(String param) {// 프로젝트별 계정정보
		MEAcntVo acntVo = null;

		String acntName = "";
		String acntTotal = "";
		String acntBalance = "";

		if (checkSuccess(param)) {
			String result[][] = parsePrint(param);
			for (int i = 0; i < result.length; i++) {
				for (int j = 0; j < result[i].length; j++) {
					if (j == 0) {
						acntName = acntName.concat(result[i][j] + ",");
					} else if (j == 1) {
						acntTotal = acntTotal.concat(result[i][j] + ",");
					} else if (j == 2) {
						acntBalance = acntBalance.concat(result[i][j] + ",");
					}
				}
			}
			result = null;
			String[] acnt_name = checkArray(acntName);
			String[] acnt_total = checkArray(acntTotal);
			String[] acnt_balance = checkArray(acntBalance);
			acntVo = new MEAcntVo();
			acntVo.setAcnt_name(acnt_name);
			acntVo.setAcnt_total(acnt_total);
			acntVo.setAcnt_balance(acnt_balance);
		}
		param = null;
		return acntVo;
	}

	public static MEAcntVo acntBasicInfo(String param) {
		MEAcntVo acntVo = null;
		String acntCode = "";
		String acntName = "";

		if (checkSuccess(param)) {
			String result[][] = parsePrint(param);
			for (int i = 0; i < result.length; i++) {
				for (int j = 0; j < result[i].length; j++) {
					if (j == 0) {
						acntCode = acntCode.concat(result[i][j] + ",");
					} else if (j == 1) {
						acntName = acntName.concat(result[i][j] + ",");
					}
				}
			}
			result = null;
			String[] acnt_code = checkArray(acntCode);
			String[] acnt_name = checkArray(acntName);

			acntVo = new MEAcntVo();
			acntVo.setAcnt_code(acnt_code);
			acntVo.setAcnt_name(acnt_name);
		}
		param = null;
		return acntVo;
	}

	public static MEUsrVO userrInfo(String param) {// 예시샘플 삭제예정
		MEUsrVO usrVo = null;
		String id = "";
		String name = "";
		String pwd = "";
		if (checkSuccess(param)) {
			String result[][] = parsePrint(param);
			for (int i = 0; i < result.length; i++) {
				for (int j = 0; j < result[i].length; j++) {
					if (j == 0) {
						id = id.concat(result[i][j] + ",");
					} else if (j == 1) {
						name = name.concat(result[i][j] + ",");
					} else if (j == 2) {
						pwd = pwd.concat(result[i][j] + ",");
					}
				}
			}
			result = null;
			String[] usr_id = checkArray(id);
			String[] usr_name = checkArray(name);
			String[] usr_pw = checkArray(pwd);

			usrVo = new MEUsrVO();
			usrVo.setUsr_id(usr_id);
			usrVo.setUsr_name(usr_name);
			usrVo.setUsr_pw(usr_pw);
		}
		param = null;
		return usrVo;
	}

	public static String[] checkArray(String param) {// delimeter trim 처리 & 배열반환
		String[] result = null;
		if (param.endsWith(",")) {
			int a = param.lastIndexOf(",");
			param.substring(0, a - 1);
		}

		result = param.split("\\,");
		param = null;
		return result;
	}

	public static boolean checkSuccess(String param) {// cmd 체크
		if ("FAIL".equals(param.substring(0, 4))) {
			param = null;
			return false;
		} else {
			param = null;
			return true;
		}
	}

	public static String[][] parsePrint(String param) { // data matrix 생성
		String[][] result;
		String[] parsedRow = parseResultString(param, "\\^");
		String[] parsedColumn = parseResultString(parsedRow[0], "\\|");
		int rowLen = parsedRow.length;
		int colLen = parsedColumn.length;
		result = new String[rowLen][colLen];
		for (int outer = 0; outer < rowLen; outer++) {
			String[] parsedField = parseResultString(parsedRow[outer], "\\|");
			for (int inner = 0; inner < colLen; inner++) {
				if ("".equals(parsedField[inner]) || parsedField[inner] == null) {
					parsedField[inner] = " ";
				}
				result[outer][inner] = parsedField[inner];
			}
		}
		param = null;
		return result;
	}

	public static String[] parseResultString(String param, String delim) { // data matrix 1차
		return param.split(delim);
	}

	// CardBill 테이블 허가득
	public static MECardVo mjsInfo(String param) {
		MECardVo cbVo = null;
		String expBillCode = "";// 결재번호
		String expUserId = "";// 사원번호
		String expBillDate = "";// 결재일자
		String expBillAmount = "";// 결재금액
		String expShopName = "";// 결재상호
		String expShopLocation = "";// 결재장소
		String expState = ""; // State
		String expProcessDate = "";
		String expType = ""; // Type

		Log.d("mjsInfo!", "yes");

		if (checkSuccess(param)) {
			String result[][] = parsePrint(param);
			for (int i = 0; i < result.length; i++) {
				for (int j = 0; j < result[i].length; j++) {
					if (j == 0) {
						expBillCode = expBillCode.concat(result[i][j] + ",");
					} else if (j == 1) {
						expUserId = expUserId.concat(result[i][j] + ",");
					} else if (j == 2) {
						expBillDate = expBillDate.concat(result[i][j] + ",");
					} else if (j == 3) {
						expBillAmount = expBillAmount.concat(result[i][j] + ",");
					} else if (j == 4) {
						expShopName = expShopName.concat(result[i][j] + ",");
					} else if (j == 5) {
						expShopLocation = expShopLocation.concat(result[i][j] + ",");
					} else if (j == 6) {
						expState = expState.concat(result[i][j] + ",");
					} else if (j == 7) {
						expProcessDate = expProcessDate.concat(result[i][j] + ",");
					} else if (j == 8) {
						expType = expType.concat(result[i][j] + ",");
					}
				}
			}
			result = null;
			String[] exp_billCode = checkArray(expBillCode);
			String[] exp_billDate = checkArray(expBillDate);
			String[] exp_billAmount = checkArray(expBillAmount);
			String[] exp_shopName = checkArray(expShopName);
			String[] exp_shopLocation = checkArray(expShopLocation);
			String[] exp_state = checkArray(expState);
			String[] exp_processDate = checkArray(expProcessDate);
			String[] exp_type = checkArray(expType);

			cbVo = new MECardVo();
			cbVo.setExp_billCode(exp_billCode);
			cbVo.setExp_billDate(exp_billDate);
			cbVo.setExp_billAmount(exp_billAmount);
			cbVo.setExp_shopName(exp_shopName);
			cbVo.setExp_shopLocation(exp_shopLocation);
			cbVo.setExp_state(exp_state);
			cbVo.setExp_processDate(exp_processDate);
			cbVo.setExp_type(exp_type);

		}
		param = null;
		return cbVo;
	}

	// 금액에 콤마 찍어주기
	public static String addComma(String str) {

		StringBuffer sb = new StringBuffer(str);
		StringBuffer rsb = new StringBuffer();
		String commaInNumber2 = "";
		sb = sb.reverse();
		int p = 0;

		for (int i = 0; i < str.length(); i++) {
			p = i % 3;

			if (i > 0) {
				if (p == 0) {
					rsb.append(",");
				}
			}
			rsb.append(sb.substring(i, (i + 1)));
		}
		String commaInNumber = (rsb.reverse()).toString();

		if (commaInNumber.lastIndexOf("-,") != -1) {
			String temp = commaInNumber.substring(2);
			String minus = "-";
			commaInNumber2 = minus.concat(temp);
			return commaInNumber2;

		} else {
			return commaInNumber;
		}
	}
}
